Spring Data CrudRepository saveAll() and findAll()

您所在的位置:网站首页 jpa saveall Spring Data CrudRepository saveAll() and findAll()

Spring Data CrudRepository saveAll() and findAll()

2024-07-17 02:16:09| 来源: 网络整理| 查看: 265

In this article, we will see about Spring Data CrudRepository saveAll() and findAll() Methods Example using Spring Boot and Oracle.

Spring Data JPA Interview Questions and AnswersHow to write a custom method in the repository in Spring Data JPA

CrudRepository interface extends the Repository interface. In Spring Data JPA Repository is a top-level interface in hierarchy. The saveAll() method has been defined as below.

Iterable saveAll(Iterable entities) – used to save multiple entities.The saveAll() method internally annotated with @Transactional. See a depth tutorial which explains why we need @Transactional annotation here.

The implementation of CrudRepository’s saveAll() method has been given in SimpleJpaRepository.java and saveAll() method internally uses save() method only as below.

@Transactional public List saveAll(Iterable entities) { List result = new ArrayList(); for (S entity : entities) { result.add(save(entity)); } return result; }

Just an additional note the CrudRepository’s save() method is used to perform save as well as an update operation. If we try to save entity first time then persist() method will get invoked and if we try to update the same entity merge() will get invoked. Observe the below implementation of save() method.

public S save(S entity) { if (entityInformation.isNew(entity)) { em.persist(entity); return entity; } else { return em.merge(entity); } } Note - See a depth tutorial that explains differences between JPA and Hibernate.

The findAll() method has been defined as below.

Iterable findAll();

The findAll() internally defined as below.

public List findAll() { return getQuery(null, Sort.unsorted()).getResultList(); }

Let’s see in below code how we are going to use the CrudRepository’s saveAll() and findAll() methods.

package com.javatute.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.javatute.entity.Student; import com.javatute.repository.StudentRepository; import com.javatute.service.StudentService; @Service("studentServiceImpl") public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; @Transactional public List saveAllStudent(List studentList) { List response = (List) studentRepository.saveAll(studentList); return response; } @Transactional public List getAllStudents() { List studentResponse = (List) studentRepository.findAll(); return studentResponse; } } Let’s see an example of Spring Data CrudRepository saveAll() and findAll() Methods for create and get the entities.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – springdatasaveall, ArtifactId – springdatasaveall and name – springdatasaveall) and click on finish. Keep packaging as the jar.

Modify pom.xml

4.0.0 springdatasaveall springdatasaveall 0.0.1-SNAPSHOT springdatasaveall org.springframework.boot spring-boot-starter-parent 2.0.2.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.oracle ojdbc6 11.2.0.3 ${project.artifactId} maven-compiler-plugin 3.1 true C:\Program Files\Java\jdk1.8.0_131\bin\javac.exe

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

Spring Data CrudRepository save() Method

Student.java

package com.javatute.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "name") private String name; @Column(name = "roll_number") private String rollNumber; @Column(name = "university") String university; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRollNumber() { return rollNumber; } public void setRollNumber(String rollNumber) { this.rollNumber = rollNumber; } public String getUniversity() { return university; } public void setUniversity(String university) { this.university = university; } }

StudentController.java

package com.javatute.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.javatute.entity.Student; import com.javatute.service.StudentService; @RestController @RequestMapping(value = "/student") public class StudentController { @Autowired private StudentService studentService; @RequestMapping(value = "/saveall", method = RequestMethod.POST) @ResponseBody public List saveAllStudents(@RequestBody List studentList) { List studentResponse = (List) studentService.saveAllStudent(studentList); return studentResponse; } @RequestMapping(value = "/getall", method = RequestMethod.GET) @ResponseBody public List getAllStudents() { List studentResponse = (List) studentService.getAllStudents(); return studentResponse; } }

See more details about @Controller and RestController here.

StudentRepository.java – interface

package com.javatute.repository; import java.io.Serializable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import com.javatute.entity.Student; @Repository public interface StudentRepository extends CrudRepository { }

StudentService.java – interface

package com.javatute.service; import java.util.List; import org.springframework.stereotype.Component; import com.javatute.entity.Student; @Component public interface StudentService { public List saveAllStudent(List studentList); public List getAllStudents(); }

StudentServiceImpl.java

package com.javatute.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.javatute.entity.Student; import com.javatute.repository.StudentRepository; import com.javatute.service.StudentService; @Service("studentServiceImpl") public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; @Transactional public List saveAllStudent(List studentList) { List response = (List) studentRepository.saveAll(studentList); return response; } @Transactional public List getAllStudents() { List studentResponse = (List) studentRepository.findAll(); return studentResponse; } }

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

SpringMain.java

package com.javatute.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.*") @EntityScan("com.javatute.entity") public class SpringMain { public static void main(String[] args) { SpringApplication.run(SpringMain.class, args); } }

Note – See more details about @ComponentScan here.

JpaConfig.java

package com.javatute.config; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration @EnableJpaRepositories(basePackages = "com.javatute.repository") public class JpaConfig { }

Note – See more details about @Configuration annotations here.

application.properties

# Connection url for the database spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE spring.datasource.username=SYSTEM spring.datasource.password=oracle2 spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver # Show or not log for each sql query spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto =create spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect server.port = 9091

Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API.

http://localhost:9091/student/saveall

Request Data –

[ { "name": "Hiteshdo", "rollNumber": "0126CS01", "university":"rgtu" }, { "name": "Johnhjhjhjhj", "rollNumber": "0126CS02", "university":"rgtu" }, { "name": "Mohankkkkkkkkkkkkkk", "rollNumber": "0126CS03", "university":"rgtu" }, { "name": "Nagesh", "rollNumber": "0126CS04", "university":"rgtu" }, { "name": "s", "rollNumber": "0126CS05", "university":"rgtu" }, { "name": "Ranakum", "rollNumber": "0126CS06", "university":"rgtu" }, { "name": "Roc", "rollNumber": "0126CS07", "university":"rgtu" }, { "name": "Simpy", "rollNumber": "0126CS08", "university":"rgtu" }, { "name": "Tiwari", "rollNumber": "0126CS09", "university":"rgtu" }, { "name": "Appu", "rollNumber": "0126CS10", "university":"rgtu" }, { "name": "Babloo", "rollNumber": "0126CS11", "university":"rgtu" }, { "name": "Ga", "rollNumber": "0126CS12", "university":"rgtu" } ]

Response Data –

[ { "id": 1, "name": "Hiteshdo", "rollNumber": "0126CS01", "university": "rgtu" }, { "id": 2, "name": "Johnhjhjhjhj", "rollNumber": "0126CS02", "university": "rgtu" }, { "id": 3, "name": "Mohankkkkkkkkkkkkkk", "rollNumber": "0126CS03", "university": "rgtu" }, { "id": 4, "name": "Nagesh", "rollNumber": "0126CS04", "university": "rgtu" }, { "id": 5, "name": "s", "rollNumber": "0126CS05", "university": "rgtu" }, { "id": 6, "name": "Ranakum", "rollNumber": "0126CS06", "university": "rgtu" }, { "id": 7, "name": "Roc", "rollNumber": "0126CS07", "university": "rgtu" }, { "id": 8, "name": "Simpy", "rollNumber": "0126CS08", "university": "rgtu" }, { "id": 9, "name": "Tiwari", "rollNumber": "0126CS09", "university": "rgtu" }, { "id": 10, "name": "Appu", "rollNumber": "0126CS10", "university": "rgtu" }, { "id": 11, "name": "Babloo", "rollNumber": "0126CS11", "university": "rgtu" }, { "id": 12, "name": "Ga", "rollNumber": "0126CS12", "university": "rgtu" } ] http://localhost:9091/student/getall [ { "id": 1, "name": "Hiteshdo", "rollNumber": "0126CS01", "university": "rgtu" }, { "id": 2, "name": "Johnhjhjhjhj", "rollNumber": "0126CS02", "university": "rgtu" }, { "id": 3, "name": "Mohankkkkkkkkkkkkkk", "rollNumber": "0126CS03", "university": "rgtu" }, { "id": 4, "name": "Nagesh", "rollNumber": "0126CS04", "university": "rgtu" }, { "id": 5, "name": "s", "rollNumber": "0126CS05", "university": "rgtu" }, { "id": 6, "name": "Ranakum", "rollNumber": "0126CS06", "university": "rgtu" }, { "id": 7, "name": "Roc", "rollNumber": "0126CS07", "university": "rgtu" }, { "id": 8, "name": "Simpy", "rollNumber": "0126CS08", "university": "rgtu" }, { "id": 9, "name": "Tiwari", "rollNumber": "0126CS09", "university": "rgtu" }, { "id": 10, "name": "Appu", "rollNumber": "0126CS10", "university": "rgtu" }, { "id": 11, "name": "Babloo", "rollNumber": "0126CS11", "university": "rgtu" }, { "id": 12, "name": "Ga", "rollNumber": "0126CS12", "university": "rgtu" } ] Spring Data CrudRepository saveAll() and findAll() Spring Data CrudRepository saveAll() and findAll()

That’s all about Spring Data CrudRepository saveAll() and findAll() Method Example Using Spring Boot and Oracle.

You may like –

Spring Data CrudRepository save() Method.Spring Data JPA example using spring boot.What is spring data JPA and what are the benefits.Sorting in Spring Data JPA using Spring Boot.@Version Annotation Example In Hibernate.Hibernate Validator Constraints Example Using Spring Boot.@Temporal Annotation Example In Hibernate/Jpa Using Spring Boot.Hibernate Table Per Concrete Class Spring Boot.Hibernate Table Per Subclass Inheritance Spring Boot.Hibernate Single Table Inheritance using Spring Boot.One To One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle.One To One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle.One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.

Spring Data CrudRepository  Docs.



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭